package cn.stylefeng.guns.modular.recy.controller;
/*
 */

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.modular.system.controller.UserMgrController;
import cn.stylefeng.guns.modular.system.entity.User;
import cn.stylefeng.guns.modular.system.service.UserService;
import cn.stylefeng.guns.modular.weixin.utils.Utils;
import cn.stylefeng.roses.core.base.controller.*;
import com.google.common.collect.Lists;
import org.springframework.stereotype.*;
import cn.stylefeng.guns.modular.recy.service.*;
import org.springframework.beans.factory.annotation.*;
import cn.stylefeng.roses.core.reqres.response.*;
import cn.stylefeng.guns.modular.recy.entity.*;
import java.io.*;
import cn.stylefeng.guns.core.common.page.*;
import com.baomidou.mybatisplus.core.metadata.*;
import com.baomidou.mybatisplus.core.conditions.*;
import cn.stylefeng.guns.modular.recy.vo.*;
import cn.stylefeng.guns.modular.recy.utils.*;
import cn.hutool.core.bean.*;
import java.util.stream.*;
import cn.stylefeng.guns.modular.recy.warpper.*;
import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import java.util.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping({ "/wasteStation" })
public class WasteStationController extends BaseController
{
    private String PREFIX;
    @Autowired
    private WasteStationService wasteStationService;

    @Autowired
    private UserService userService;

    @Autowired
    private RegionService regionService;
    
    public WasteStationController() {
        this.PREFIX = "/modular/recy/wasteStation/";
    }
    
    @RequestMapping({ "" })
    public String index() {
        return this.PREFIX + "wasteStation.html";
    }
    
    @RequestMapping({ "/add" })
    public String wasteStationAdd(Model model) {
        List<User> list = this.userService.list();
        model.addAttribute("userList",list);
        return this.PREFIX + "wasteStation_add.html";
    }
    
    @RequestMapping({ "/edit" })
    public String wasteStationUpdate(@RequestParam("id") final String id) {
        return this.PREFIX + "wasteStation_edit.html";
    }
    
    @RequestMapping({ "/detail/{id}" })
    @ResponseBody
    public ResponseData detail(@PathVariable final String id) {
        final WasteStation wasteStation = (WasteStation)this.wasteStationService.getById((Serializable)id);
        return (ResponseData)ResponseData.success(wasteStation);
    }
    
    @RequestMapping({ "/list" })
    @ResponseBody
    public Object list(final WasteStation wasteStationQuery) {
        final QueryWrapper<WasteStation> wasteStationQueryWrapper = ControllerUtil.createQuery(wasteStationQuery, this.getHttpServletRequest());
        final Page<WasteStation> page = (Page<WasteStation>)LayuiPageFactory.defaultPage();
        final IPage<WasteStation> page2 = (IPage<WasteStation>)this.wasteStationService.page((IPage)page, (Wrapper)wasteStationQueryWrapper);
        final List<WasteStationVO> wasteStationVOList = EntityBind.bind(page2.getRecords(), WasteStationVO.class);
        final List<Map<String, Object>> list = wasteStationVOList.stream().map(wasteStation -> BeanUtil.beanToMap(wasteStation)).collect(Collectors.toList());
        page2.setRecords((List)new WasteStationWrapper(list).wrap());
        return LayuiPageFactory.createPageInfo(page2);
    }
    
    @RequestMapping({ "/addItem" })
    @ResponseBody
    public Object add(final WasteStation wasteStation) {
        final HttpServletRequest request = this.getHttpServletRequest();
        final StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
        final MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
        final MultipartFile file = multipartRequest.getFile("file");
        try {
            final String name = file.getOriginalFilename();
            final InputStream inputStream = file.getInputStream();
            String fileName = ControllerUtil.saveFile("/upload/brand", inputStream, name);
            wasteStation.setPhoto(fileName);

        }
        catch (IOException e) {
            e.printStackTrace();
        }
        String areaCode = wasteStation.getAreaCode();
        String area = Utils.areaFullName(areaCode, regionService);
        wasteStation.setArea(area);

        this.wasteStationService.save(wasteStation);
        return WasteStationController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/delete" })
    @ResponseBody
    public Object delete(@RequestParam final String id) {
        this.wasteStationService.removeById((Serializable)id);
        return WasteStationController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/editItem" })
    @ResponseBody
    public Object update(final WasteStation wasteStation) {
        this.wasteStationService.updateById(wasteStation);
        return WasteStationController.SUCCESS_TIP;
    }
    
    @RequestMapping(value = { "/delItems" }, method = { RequestMethod.POST })
    @ResponseBody
    public Object delItem(@RequestBody final String[] ids) {
        this.wasteStationService.removeByIds((Collection)Arrays.asList(ids));
        return WasteStationController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/show" })
    public Object show(final String id, final Model model) {
        final WasteStation wasteStation = (WasteStation)this.wasteStationService.getById((Serializable)id);
        final Map map = BeanUtil.beanToMap(wasteStation);
        model.addAttribute("wasteStation", new WasteStationWrapper(map).wrap());
        return this.PREFIX + "wasteStation_show.html";
    }

    @RequestMapping("/searchByArea")
    @ResponseBody
    public Object searchByArea(String areaCode){
        if(StrUtil.isBlank(areaCode)){
            return Lists.newArrayList();
        }
        QueryWrapper<WasteStation> wasteStationQueryWrapper = new QueryWrapper<>();
        wasteStationQueryWrapper.lambda().eq(WasteStation::getAreaCode,areaCode);
        List<WasteStation> wasteStationList = this.wasteStationService.list(wasteStationQueryWrapper);
        return wasteStationList;
    }
}
